package com.jojo.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jojo.store.controller.ex.ProductNotFoundException;
import com.jojo.store.entity.Product;
import com.jojo.store.mapper.ProductMapper;
import com.jojo.store.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceIMpl implements ProductService {
    @Autowired
    private  ProductMapper productMapper;
    @Override
    public List<Product> findAllHotPro() {
        List<Product> products = productMapper.selectHotList();
        //传过去的products不需要那么多数据信息，只保留关键的减少服务器压力；
        for (Product product : products) {
            product.setPriority(null);
            product.setCategoryId(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedTime(null);
            product.setModifiedUser(null);
        }
        return products;
    }

    @Override
    public Product findByid(Integer id ) {
        Product idresult = productMapper.selectOne(new QueryWrapper<Product>().eq("id", id));
        if (idresult==null){
            throw new ProductNotFoundException("商品数据不存在");
        }
        return idresult;
    }
}
